package com.yuexun.beilunpatient.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class URLImageGetter implements Html.ImageGetter {
    TextView mTextView;
    ImageLoader imageLoader;

    public URLImageGetter(TextView textView) {
        this.mTextView = textView;
        imageLoader = ImageLoader.getInstance();
    }

    public URLImageGetter(TextView textView, Context context) {
        this.mTextView = textView;
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }

    @Override
    public Drawable getDrawable(String source) {
        final URLDrawable urlDrawable = new URLDrawable();
        imageLoader.loadImage(source, new SimpleImageLoadingListener() {
              @Override
              public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                  urlDrawable.bitmap = loadedImage;
                  urlDrawable.setBounds(0, 0, loadedImage.getWidth(), loadedImage.getHeight());
                  mTextView.invalidate();
                  mTextView.setText(mTextView.getText()); // 解决图文重叠
              }
        });
        return urlDrawable;
    }
}